package com.shuqi.model.parse.parser;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import com.shuqi.model.bean.AccountBalanceInfo;
import com.shuqi.model.parse.HandlerBase;

/**
 * 获取余额接口 author yanghuawei
 */
public class AccountBalancePaser extends HandlerBase {

    private int which = 0;

    private final int BANLANCE = 1;
    private AccountBalanceInfo info;

    @Override
    public Object getParsedData() {
        return info;
    }

    @Override
    public void startDocument() throws SAXException {
        info = new AccountBalanceInfo();
        super.startDocument();
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes)
            throws SAXException {
        if ("serverinfo".equals(localName)) {
            info.setMsgCode(attributes.getValue("state"));
            which = 0;
        } else if ("Banlance".equals(localName)) {
            which = BANLANCE;
        } else {
            which = 0;
        }
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        super.characters(ch, start, length);
        String value = new String(ch, start, length);
        if (value != null && !value.trim().equals("")) {
            if (which == BANLANCE) {
                if (info.getBalance() != null) {
                    info.setBalance(info.getBalance() + value);
                } else {
                    info.setBalance(value);
                }
            }
        }
    }

    @Override
    public void endDocument() throws SAXException {
        which = 0;
        super.endDocument();
    }

}
